package com.uam.db.model.vo;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.uam.db.model.entity.User;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author GoryLee
 * @Date 2022/12/5 01:57
 * @Version 1.0
 */
@Data
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private User user;
    private Set<String> roleName;

    private Set<String> permissions;

    private Set<String> urls;
    private Set<Long> ids;
    public LoginUser(User user, Set<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }

    public LoginUser(User user, Set<String> permissions, Set<String> urls) {
        this.user = user;
        this.permissions = permissions;
        this.urls = urls;
    }
    public LoginUser(User user, Set<String> permissions, Set<String> urls,Set<String> roleName,Set<Long> ids) {
        this.user = user;
        this.permissions = permissions;
        this.urls = urls;
        this.roleName = roleName;
        this.ids = ids;
    }
    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (CollUtil.isNotEmpty(authorities)) {
            return authorities;
        }

        return permissions.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toSet());
    }

    @Override
    public String getPassword() {
        return user != null ? user.getPassword() : null;
    }

    @Override
    public String getUsername() {
        return user != null ? user.getUserName() : null;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
